package com.example.mid_test.controller;

import com.example.mid_test.model.Student;
import com.example.mid_test.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

public class StudentController {

    @Autowired
    StudentService studentService;

    @GetMapping("/Allstu")
    @CrossOrigin(origins = "http://localhost:8888")
    public List<Student> getAllStu(){
        List<Student> list = studentService.list();
        return list;
    }

    @GetMapping("/stu/{id}")
    public Student getStuById(@PathVariable("id") Integer id){
        Student byId = studentService.getById(id);
        return byId;
    }

    @PostMapping("/stu")
    public void addStu(Student student){
        studentService.save(student);
    }

    @PutMapping("/stu")
    public void updateStu(Student student){
        studentService.updateById(student);
    }

    @DeleteMapping("/stu")
    public void deleteStu(@RequestParam Integer id){
        studentService.removeById(id);
    }
}
